<template>
  <div class="tags">
    <el-tag
        v-for="(tag,index) in tags" :key="tag.path"
        size="small"
        :closable="tag.name!=='home'"
        :effect="$route.name === tag.name? 'dark':'plain'"
        @click="changeMenu(tag)"
        @close="closeMenu(tag,index)">
      {{ tag.label }}
    </el-tag>
  </div>
</template>

<script>
import {mapState, mapMutations} from "vuex";

export default {
  name: "CommonTag",
  methods: {
    closeMenu(tag, index) {
      this.delMenu(index)
      if (tag.name === this.$route.name) { /*如果删除所在页面标签，则跳转到上一级*/
        this.$router.push({name: this.tags[index - 1].name})
      }
    },
    changeMenu(tag) {
      if (this.$route.path !== tag.path)
        this.$router.push({name: tag.name})
    },
    ...mapMutations('tab', ['delMenu'])
  },
  computed: {
    ...mapState({tags: state => state.tab.tabsList})
  }
}
</script>

<style scoped lang="less">
.tags {
  padding: 10px;

  .el-tag {
    cursor: pointer;
  }
}
</style>
